<html>
<head><meta charset="utf-8"><title>noop attribute and derive macros · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/noop.20attribute.20and.20derive.20macros.html">noop attribute and derive macros</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239476632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/noop%20attribute%20and%20derive%20macros/near/239476632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Franklin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/noop.20attribute.20and.20derive.20macros.html#239476632">(May 19 2021 at 18:45)</a>:</h4>
<p>I'm not sure if this is the right place to ask about this. I'm thinking about writing an rfc for noop attribute and derive macros. This would be useful when you want to conditionally compile generated code.</p>
<p>Instead of <code>cfg_attr</code> everywhere you use it, you could write</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg(feature = </span><span class="s">"serde"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">serde</span>::<span class="p">{</span><span class="n">Serialize</span><span class="p">,</span><span class="w"> </span><span class="n">Deserialize</span><span class="p">};</span><span class="w"></span>

<span class="cp">#[cfg(not(feature = </span><span class="s">"serde"</span><span class="cp">))]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span>::<span class="n">core</span>::<span class="n">macros</span>::<span class="n">NoOp</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">Serialize</span><span class="p">;</span><span class="w"></span>
<span class="cp">#[cfg(not(feature = </span><span class="s">"serde"</span><span class="cp">))]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span>::<span class="n">core</span>::<span class="n">macros</span>::<span class="n">NoOp</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">Deserialize</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>This would be even more useful for things where it only makes sense to enable it globally. In that case you could publish a crate that either re-exports something from a proc-macro crate or a noop.</p>
<p>My use-case is a derive macro that generates trampolines for debug/display/structured printing a type with debugger integrations to call them. Ideally library users could just stick it on everything and it would be nearly free unless the final downstream user built with debug assertions and enabled it. Credit for the idea goes to Yandros ꜰʀ-ᴇꜱ on Discord.</p>



<a name="239485091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/noop%20attribute%20and%20derive%20macros/near/239485091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/noop.20attribute.20and.20derive.20macros.html#239485091">(May 19 2021 at 19:46)</a>:</h4>
<p>I think you'd need more code than just <code>use NoOp as Serialize</code> so rustc knows what to do with the <code>#[serde(...)]</code> attributes</p>



<a name="239486331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/noop%20attribute%20and%20derive%20macros/near/239486331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Franklin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/noop.20attribute.20and.20derive.20macros.html#239486331">(May 19 2021 at 19:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="229517">Jacob Lifshay</span> <a href="#narrow/stream/219381-t-libs/topic/noop.20attribute.20and.20derive.20macros/near/239485091">said</a>:</p>
<blockquote>
<p>I think you'd need more code than just <code>use NoOp as Serialize</code> so rustc knows what to do with the <code>#[serde(...)]</code> attributes</p>
</blockquote>
<p>You're right, I forgot attributes can have arbitrary names. That makes this a little more complicated. NoOp could be special-cased as accepting any attribute, or it could be something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">proc_macro_derive_noop</span><span class="o">!</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span><span class="w"> </span><span class="n">attributes</span><span class="p">(</span><span class="n">foo</span><span class="p">));</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>